home *** CD-ROM | disk | FTP | other *** search
/ Amiga Tools 2 / Amiga Tools 2.iso / tools / packer / tar / src / getoldopt.c < prev    next >
C/C++ Source or Header  |  1995-03-09  |  1KB  |  69 lines

  1. /*
  2.  * Plug-compatible replacement for getopt() for parsing tar-like
  3.  * arguments.  If the first argument begins with "-", it uses getopt;
  4.  * otherwise, it uses the old rules used by tar, dump, and ps.
  5.  *
  6.  * Written 25 August 1985 by John Gilmore (ihnp4!hoptoad!gnu) and placed
  7.  * in the Pubic Domain for your edification and enjoyment.
  8.  *
  9.  * @(#)getoldopt.c 1.4 2/4/86 Public Domain - gnu
  10.  */
  11.  
  12. #include <stdio.h>
  13.  
  14.  
  15. int
  16. getoldopt(argc, argv, optstring)
  17.     int    argc;
  18.     char    **argv;
  19.     char    *optstring;
  20. {
  21.     extern char    *optarg;    /* Points to next arg */
  22.     extern int    optind;        /* Global argv index */
  23.     static char    *key;        /* Points to next keyletter */
  24.     static char    use_getopt;    /* !=0 if argv[1][0] was '-' */
  25.     extern char    *index();
  26.     char        c;
  27.     char        *place;
  28.  
  29.     optarg = NULL;
  30.     
  31.     if (key == NULL) {        /* First time */
  32.         if (argc < 2) return EOF;
  33.         key = argv[1];
  34.         if (*key == '-')
  35.             use_getopt++;
  36.         else
  37.             optind = 2;
  38.     }
  39.  
  40.     if (use_getopt)
  41.         return getopt(argc, argv, optstring);
  42.  
  43.     c = *key++;
  44.     if (c == '\0') {
  45.         key--;
  46.         return EOF;
  47.     }
  48.     place = index(optstring, c);
  49.  
  50.     if (place == NULL || c == ':') {
  51.         fprintf(stderr, "%s: unknown option %c\n", argv[0], c);
  52.         return('?');
  53.     }
  54.  
  55.     place++;
  56.     if (*place == ':') {
  57.         if (optind < argc) {
  58.             optarg = argv[optind];
  59.             optind++;
  60.         } else {
  61.             fprintf(stderr, "%s: %c argument missing\n",
  62.                 argv[0], c);
  63.             return('?');
  64.         }
  65.     }
  66.  
  67.     return(c);
  68. }
  69.